package com.dci.service.impl.project;

import com.dci.dao.pms.project.FeedbackInfoMapper;
import com.dci.dao.pms.project.OutboundInfoMapper;
import com.dci.dao.pms.project.PlanInfoMapper;
import com.dci.dao.pms.project.ProjectInfoMapper;
import com.dci.model.project.*;
import com.dci.result.JsonData;
import com.dci.service.project.PlanInfoService;
import com.dci.util.UUIDBuild;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service.impl.project
 * @Description: 项目计划实现类
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PlanInfoServiceImpl implements PlanInfoService {

    @Resource
    private PlanInfoMapper planInfoMapper;

    @Resource
    private FeedbackInfoMapper feedbackInfoMapper;

    @Resource
    private ProjectInfoMapper projectInfoMapper;

    @Resource
    private OutboundInfoMapper outboundInfoMapper;

    /**
     * 项目进度表,新增
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(PlanInfo record) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String time = sdf.format(date);
//        String records = time + ",管理员创建了进度表;";
        String str = record.getRemark();
        String id = record.getId();
        List list = new ArrayList<PlanInfo>();
        if (str != null && !"".equals(str)) {
            String[] strS = str.split(";");

            String mattersStr = strS[0].substring(0, strS[0].length() - 1);
            String[] matters = mattersStr.split(",");

            String planTimesStr = strS[1].substring(0, strS[1].length() - 1);
            String[] planTimes = planTimesStr.split(",");

            String departmentsStr = strS[2].substring(0, strS[2].length() - 1);
            String[] departments = departmentsStr.split(",");

            String headsStr = strS[3].substring(0, strS[3].length() - 1);
            String[] heads = headsStr.split(",");

            String remarksStr = strS[4].substring(0, strS[4].length() - 1);
            String[] remarks = remarksStr.split(",");

            // 新增
            for (int i = 0; i < matters.length; i++) {
                PlanInfo planInfo = new PlanInfo();
                //ID,UUID
                planInfo.setId(UUIDBuild.getUUID());
                // ProjectId
                planInfo.setProjectId(id);
                //添加进度记录
//                planInfo.setRecord(records);
                // 序号
                planInfo.setNo(i + 1);
                planInfo.setMatters(matters[i] != null && !"@".equals(matters[i]) ? matters[i] : "");
                planInfo.setPlanTime(planTimes[i] != null && !"@".equals(planTimes[i]) ? planTimes[i] : "");
                planInfo.setDepartment(departments[i] != null && !"@".equals(departments[i]) ? departments[i] : "");
                planInfo.setHead(heads[i] != null && !"@".equals(heads[i]) ? heads[i] : "");
                planInfo.setRemark(remarks[i] != null && !"@".equals(remarks[i]) ? remarks[i] : "");
                // 删除标记,初始0
                planInfo.setDelFlag("0");
                list.add(planInfo);
            }

            // 批量删除
            PlanInfo delPlanInfo = new PlanInfo();
            delPlanInfo.setProjectId(id);
            planInfoMapper.deleteByProjectId(delPlanInfo);

            // 批量新增
            planInfoMapper.batchInsertPlanInfo(list);
        }
        return 1;
    }

    @Override
    public int addSelective(PlanInfo record) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String time = sdf.format(date);
//        String records = time + ",管理员创建了进度表;";
        String str = record.getRemark();
        String id = record.getId();
        List list = new ArrayList<PlanInfo>();
        if (str != null && !"".equals(str)) {
            String[] strS = str.split(";");

            String mattersStr = strS[0].substring(0, strS[0].length() - 1);
            String[] matters = mattersStr.split(",");

            String planTimesStr = strS[1].substring(0, strS[1].length() - 1);
            String[] planTimes = planTimesStr.split(",");

            String departmentsStr = strS[2].substring(0, strS[2].length() - 1);
            String[] departments = departmentsStr.split(",");

            String headsStr = strS[3].substring(0, strS[3].length() - 1);
            String[] heads = headsStr.split(",");

            String remarksStr = strS[4].substring(0, strS[4].length() - 1);
            String[] remarks = remarksStr.split(",");

            String weightStr = strS[6].substring(0, strS[6].length() - 1);
            String[] weight = weightStr.split(",");

            String progressStr = strS[5].substring(0, strS[5].length() - 1);
            String[] progress = progressStr.split(",");

            // 新增
            for (int i = 0; i < matters.length; i++) {
                PlanInfo planInfo = new PlanInfo();
                //ID,UUID
                planInfo.setId(UUIDBuild.getUUID());
                // ProjectId
                planInfo.setProjectId(id);
                //添加进度记录
//                planInfo.setRecord(records);
                // 序号
                planInfo.setNo(i + 1);
                planInfo.setMatters(matters[i] != null && !"@".equals(matters[i]) ? matters[i] : "");
                planInfo.setPlanTime(planTimes[i] != null && !"@".equals(planTimes[i]) ? planTimes[i] : "");
                planInfo.setDepartment(departments[i] != null && !"@".equals(departments[i]) ? departments[i] : "");
                planInfo.setHead(heads[i] != null && !"@".equals(heads[i]) ? heads[i] : "");
                planInfo.setRemark(remarks[i] != null && !"@".equals(remarks[i]) ? remarks[i] : "");
                planInfo.setWeight(weight[i] != null && !"@".equals(weight[i]) ? weight[i] : "");
                planInfo.setProgress(progress[i] != null && !"0".equals(progress[i]) ? Integer.valueOf(progress[i]) : 0);
                // 删除标记,初始0
                planInfo.setDelFlag("0");
                list.add(planInfo);
            }

            // 批量删除
            PlanInfo delPlanInfo = new PlanInfo();
            delPlanInfo.setProjectId(id);
            planInfoMapper.deleteByProjectId(delPlanInfo);

            // 批量新增
            planInfoMapper.batchInsertPlanInfoByList(list);

            //改变项目状态
            ProjectInfo projectInfo = new ProjectInfo();
            projectInfo.setId(id);
            projectInfo.setOutboundStatus(0);
            projectInfo.setType(2);
            projectInfoMapper.updateByPrimaryKeySelective(projectInfo);
            //原因去除
            OutboundInfo outboundInfo = new OutboundInfo();
            outboundInfo.setProjectId(id);
            outboundInfoMapper.updateById(outboundInfo);
            //项目进度计算
            PlanInfo planInfo = new PlanInfo();
            planInfo.setProjectId(id);
            List<PlanInfo> listP = planInfoMapper.selectByProgress(planInfo);
            double progress1 = 0;
            for (PlanInfo plan : listP) {
                if (plan.getProgress() != null) {
                    int weight1 = Integer.valueOf(plan.getWeight());
                    int gress = plan.getProgress();
                    progress1 = (double) weight1 * (double) gress * 0.01 + progress1;
                }
            }
            int progress_number = (int) progress1;
            ProjectInfo projectInfo1 = new ProjectInfo();
            projectInfo1.setId(id);
            projectInfo1.setProgress_number(progress_number);
            projectInfoMapper.updateByProjectId(projectInfo1);
        }
        return 1;
    }


    /**
     * 根据 ProjectId 查询相关进度计划表信息
     *
     * @param record
     * @return
     */
    @Override
    public JsonData selectByProjectId(ProjectInfo record) {
        PlanInfo planInfo = new PlanInfo();
        planInfo.setProjectId(record.getId());
        List<PlanInfo> list = planInfoMapper.selectByProjectId(planInfo);
        if (list.size() > 0) {
            for (PlanInfo p : list) {
                List<FeedbackInfo> list1 = feedbackInfoMapper.selectById(p.getId());
                p.setListFeed(list1);
            }
        }
        return JsonData.success(list, 906);
    }


    /**
     * 批量更新
     *
     * @param list
     * @return
     */
    @Override
    public JsonData batchUpdatePlanInfo(List<PlanInfo> list) {
        List<PlanInfo> lists = new ArrayList<>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String da = sdf.format(date);
        String record = "";
        for (PlanInfo li : list) {
            String records = li.getRecord() + record;
            li.setRecord(records);
            lists.add(li);
        }
        planInfoMapper.batchUpdatePlanInfo(lists);
        return JsonData.success(lists, 906);
    }


    /**
     * 根据项目Id 批量删除
     *
     * @param record
     * @return
     */
    @Override
    public int deleteByProjectId(PlanInfo record) {
        planInfoMapper.deleteByProjectId(record);
        return 0;
    }


    @Override
    public PlanInfo selectByPrimaryKey(PlanInfoKey key) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(PlanInfo record) {
        return 0;
    }

    @Override
    public JsonData selectPlanPages(PlanInfo record) {
        return null;
    }

    /**
     * 批量新增
     *
     * @param list
     * @return
     */
    @Override
    public int batchInsertPlanInfo(List<PlanInfo> list) {
        return 0;
    }


}
